home *** CD-ROM | disk | FTP | other *** search
/ Macwelt 1 / Macwelt DVD 1.toast / Web-Publishing / HTML-Editoren / Alpha ƒ / Mode Examples / Java-Example.java < prev    next >
Encoding:
Java Source  |  2000-10-30  |  7.7 KB  |  181 lines

  1. // Java-Example.java
  2. // 
  3. // Included in the Alpha distribution as an example of the Java mode.
  4. // 
  5. // Source of original document:
  6. // 
  7. // http://www.npac.syr.edu/projects/tutorials/books/javanut/
  8.  
  9. // This example is from the book _Java in a Nutshell_ by David Flanagan.
  10. // Written by David Flanagan.  Copyright (c) 1996-2000 O'Reilly & Associates.
  11. // You may study, use, modify, and distribute this example for any purpose.
  12. // This example is provided WITHOUT WARRANTY either expressed or implied.
  13.  
  14. import java.awt.*;
  15.  
  16. public class AllComponents extends Frame {
  17.     MenuBar menubar;                // the menubar
  18.     Menu file, help;                // menu panes
  19.     Button okay, cancel;            // buttons
  20.     List list;                      // A list of choices
  21.     Choice choice;                  // A menu of choices
  22.     CheckboxGroup checkbox_group;   // A group of button choices
  23.     Checkbox[] checkboxes;          // the buttons to choose from
  24.     TextField textfield;            // One line of text input
  25.     TextArea  textarea;             // A text window
  26.     ScrollableScribble scribble;    // An area to draw in.
  27.     FileDialog file_dialog;
  28.     
  29.     Panel panel1, panel2 ;    // Sub-containers for all this stuff.
  30.     Panel buttonpanel; 
  31.  
  32.     // The layout manager for each of the containers.
  33.     GridBagLayout gridbag = new GridBagLayout();
  34.     
  35.     public AllComponents(String title) {
  36.         super(title);
  37.         
  38.         // Create the menubar.  Tell the frame about it.
  39.         menubar = new MenuBar();
  40.         this.setMenuBar(menubar);
  41.         // Create the file menu.  Add two items to it.  Add to menubar.
  42.         file = new Menu("File");
  43.         file.add(new MenuItem("Open"));
  44.         file.add(new MenuItem("Quit"));
  45.         menubar.add(file);
  46.         // Create Help menu; add an item; add to menubar
  47.         help = new Menu("Help");
  48.         help.add(new MenuItem("About"));
  49.         menubar.add(help);
  50.         // Display the help menu in a special reserved place.
  51.         menubar.setHelpMenu(help);
  52.         
  53.         // Create pushbuttons
  54.         okay = new Button("Okay");
  55.         cancel = new Button("Cancel");
  56.         
  57.         // Create a menu of choices
  58.         choice = new Choice();
  59.         choice.addItem("red");
  60.         choice.addItem("green");
  61.         choice.addItem("blue");
  62.         
  63.         // Create checkboxes, and group them.
  64.         checkbox_group = new CheckboxGroup();
  65.         checkboxes = new Checkbox[3];
  66.         checkboxes[0] = new Checkbox("vanilla", checkbox_group, false);
  67.         checkboxes[1] = new Checkbox("chocolate", checkbox_group, true);
  68.         checkboxes[2] = new Checkbox("strawberry", checkbox_group, false);
  69.         
  70.         // Create a list of choices.
  71.         list = new List(4, true);
  72.         list.addItem("Java"); list.addItem("C"); list.addItem("C++");
  73.         list.addItem("Smalltalk"); list.addItem("Lisp");
  74.         list.addItem("Modula-3"); list.addItem("Forth");
  75.         
  76.         // Create a one-line text field, and multi-line text area.
  77.         textfield = new TextField(15);
  78.         textarea = new TextArea(6, 40);
  79.         textarea.setEditable(false);
  80.         
  81.         // Create a scrolling canvas to scribble in. 
  82.         scribble = new ScrollableScribble();
  83.         
  84.         // Create a file selection dialog box
  85.         file_dialog = new FileDialog(this, "Open File", FileDialog.LOAD);
  86.         
  87.         // Create a Panel to contain all the components along the
  88.         // left hand side of the window.  Use a GridBagLayout for it.
  89.         panel1 = new Panel();
  90.         panel1.setLayout(gridbag);
  91.         
  92.         // Use several versions of the constrain() convenience method
  93.         // to add components to the panel and to specify their 
  94.         // GridBagConstraints values.
  95.         constrain(panel1, new Label("Name:"), 0, 0, 1, 1);
  96.         constrain(panel1, textfield, 0, 1, 1, 1);
  97.         constrain(panel1, new Label("Favorite color:"), 0, 2, 1, 1, 
  98.               10, 0, 0, 0);
  99.         constrain(panel1, choice, 0, 3, 1, 1);
  100.         constrain(panel1, new Label("Favorite flavor:"), 0, 4, 1, 1, 
  101.               10, 0, 0, 0);
  102.         constrain(panel1, checkboxes[0], 0, 5, 1, 1);
  103.         constrain(panel1, checkboxes[1], 0, 6, 1, 1);
  104.         constrain(panel1, checkboxes[2], 0, 7, 1, 1);
  105.         constrain(panel1, new Label("Favorite languages:"), 0, 8, 1, 1,
  106.               10, 0, 0, 0);
  107.         constrain(panel1, list, 0, 9, 1, 3, GridBagConstraints.VERTICAL,
  108.               GridBagConstraints.NORTHWEST, 0.0, 1.0, 0, 0, 0, 0);
  109.         
  110.         // Create a panel for the items along the right side.
  111.         // Use a GridBagLayout, and arrange items with constrain(), as above.
  112.         panel2 = new Panel();
  113.         panel2.setLayout(gridbag);
  114.         
  115.         constrain(panel2, new Label("Messages"), 0, 0, 1, 1);
  116.         constrain(panel2, textarea, 0, 1, 1, 3, GridBagConstraints.HORIZONTAL,
  117.               GridBagConstraints.NORTH, 1.0, 0.0, 0, 0, 0, 0);
  118.         constrain(panel2, new Label("Diagram"), 0, 4, 1, 1, 10, 0, 0, 0);
  119.         constrain(panel2, scribble, 0, 5, 1, 5, GridBagConstraints.BOTH,
  120.               GridBagConstraints.CENTER, 1.0, 1.0, 0, 0, 0, 0);
  121.         
  122.         // Do the same for the buttons along the bottom.
  123.         buttonpanel = new Panel();
  124.         buttonpanel.setLayout(gridbag);
  125.         constrain(buttonpanel, okay, 0, 0, 1, 1, GridBagConstraints.NONE,
  126.               GridBagConstraints.CENTER, 0.3, 0.0, 0, 0, 0, 0);
  127.         constrain(buttonpanel, cancel, 1, 0, 1, 1, GridBagConstraints.NONE,
  128.               GridBagConstraints.CENTER, 0.3, 0.0, 0, 0, 0, 0);
  129.         
  130.         // Finally, use a GridBagLayout to arrange the panels themselves
  131.         this.setLayout(gridbag);
  132.         // And add the panels to the toplevel window
  133.         constrain(this, panel1, 0, 0, 1, 1, GridBagConstraints.VERTICAL, 
  134.               GridBagConstraints.NORTHWEST, 0.0, 1.0, 10, 10, 5, 5);
  135.         constrain(this, panel2, 1, 0, 1, 1, GridBagConstraints.BOTH,
  136.               GridBagConstraints.CENTER, 1.0, 1.0, 10, 10, 5, 10);
  137.         constrain(this, buttonpanel, 0, 1, 2, 1, GridBagConstraints.HORIZONTAL,
  138.               GridBagConstraints.CENTER, 1.0, 0.0, 5, 0, 0, 0);
  139.     }
  140.     
  141.     public void constrain(Container container, Component component, 
  142.                   int grid_x, int grid_y, int grid_width, int grid_height,
  143.                   int fill, int anchor, double weight_x, double weight_y,
  144.                   int top, int left, int bottom, int right)
  145.     {
  146.         GridBagConstraints c = new GridBagConstraints();
  147.         c.gridx = grid_x; c.gridy = grid_y;
  148.         c.gridwidth = grid_width; c.gridheight = grid_height;
  149.         c.fill = fill; c.anchor = anchor;
  150.         c.weightx = weight_x; c.weighty = weight_y;
  151.         if (top+bottom+left+right > 0)
  152.             c.insets = new Insets(top, left, bottom, right);
  153.         
  154.         ((GridBagLayout)container.getLayout()).setConstraints(component, c);
  155.         container.add(component);
  156.     }
  157.     
  158.     public void constrain(Container container, Component component, 
  159.                   int grid_x, int grid_y, int grid_width, int grid_height) {
  160.         constrain(container, component, grid_x, grid_y, 
  161.               grid_width, grid_height, GridBagConstraints.NONE, 
  162.               GridBagConstraints.NORTHWEST, 0.0, 0.0, 0, 0, 0, 0);
  163.     }
  164.     
  165.     public void constrain(Container container, Component component, 
  166.                   int grid_x, int grid_y, int grid_width, int grid_height,
  167.                   int top, int left, int bottom, int right) {
  168.         constrain(container, component, grid_x, grid_y, 
  169.               grid_width, grid_height, GridBagConstraints.NONE, 
  170.               GridBagConstraints.NORTHWEST, 
  171.               0.0, 0.0, top, left, bottom, right);
  172.     }
  173.     
  174.     public static void main(String[] args) {
  175.         Frame f = new AllComponents("AWT Demo");
  176.         // We should call f.pack() here.  But its buggy.
  177.         f.resize(450, 475);
  178.         f.show();
  179.     }
  180. }
  181.